/* Definitions for dynamic library loading.

   This file is part of khipu.

   khipu is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include "dynlib.hpp"
#include "interp.hpp"

#ifdef KP_STATIC

KP_DECLS_BEGIN

void* dynlib_open (interpreter *, const char *, uint32_t, uint32_t)
{
  return (nullptr);
}

void* dynlib_sym (void *, const char *)
{
  return (nullptr);
}

void dynlib_close (void *)
{
}

const char* dynlib_extension ()
{
  return ("");
}

#elif defined (KP_PLATFORM_WINDOWS)

#  define WIN32_LEAN_AND_MEAN
  #include <windows.h>

KP_DECLS_BEGIN

static void*
win32_open (const char *src, int len, void *space)
{
  wchar_t *outp = (wchar_t *)space;

  if (MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS,
      src, len, outp, INT_MAX))
    return (LoadLibraryW (outp));
  else
    return (nullptr);
}

void* dynlib_open (interpreter *interp,
  const char *path, uint32_t len, uint32_t flags)
{
  if (!path)
    return (GetModuleHandle (nullptr));

  tmp_allocator ta { interp };
  void *space = nullptr;
  const char *mpath = path;

  if ((flags & DYNLIB_APPEXT) && path != nullptr &&
      len > 4 && memcmp (path + len - 4, ".dll", 4) != 0)
    {
      space = ta.alloc (4 * (len + 5));
      mpath = (const char *)memcpy (space, path, len);
      memcpy ((char *)space + len, ".dll", 5);
      space = (char *)space + len + 5;
      len += 4;
    }
  else
    space = ta.alloc (len * 4);

  return (win32_open (mpath, len, space));
}

void* dynlib_sym (void *dynlib, const char *symbol)
{
  return ((void *)GetProcAddress ((HMODULE)dynlib, symbol));
}

void dynlib_close (void *dynlib)
{
  if (dynlib != GetModuleHandle (nullptr))
    FreeLibrary ((HMODULE)dynlib);
}

const char* dynlib_extension ()
{
  return (".dll");
}

#else

#include <dlfcn.h>

KP_DECLS_BEGIN

void* dynlib_open (interpreter *interp,
  const char *path, uint32_t len, uint32_t flags)
{
  tmp_allocator ta { interp };
  const char *mpath = path;

  if ((flags & DYNLIB_APPEXT) && path != nullptr &&
      len > 3 && memcmp (path + len - 3, ".so", 3) != 0)
    {
      void *space = ta.alloc (len + 4);
      mpath = (const char *)memcpy (space, path, len);
      memcpy ((char *)path + len, ".so", 3);
    }

  int rf = RTLD_NOW | ((flags & DYNLIB_LOCAL) ? RTLD_LOCAL : RTLD_GLOBAL);
  return (dlopen (mpath, rf));
}

void* dynlib_sym (void *dynlib, const char *symbol)
{
  return (dlsym (dynlib, symbol));
}

void dynlib_close (void *dynlib)
{
  dlclose (dynlib);
}

const char* dynlib_extension ()
{
  return (".so");
}

#endif

void* dynlib_open (interpreter *interp, const char *path, uint32_t flags)
{
  return (dynlib_open (interp, path, path ? strlen (path) : 0, flags));
}

KP_DECLS_END
